/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.security.managedbean.function;

//import com.telsoft.security.business.SEC_BU_GROUP_RIGHT;
//import com.telsoft.security.business.SEC_BU_USER_GROUP;
import com.telsoft.security.business.SEC_BU_USER_GROUP;
import com.telsoft.security.business.SEC_BU_User;
import com.telsoft.security.entity.SEC_ETT_Group;
import com.telsoft.security.entity.SEC_ETT_User;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.NoResultException;
import javax.servlet.http.HttpSession;

/**
 *
 * @author nguyenthang
 */
@ManagedBean(name = "secuser")
@SessionScoped
public class User implements Serializable {
    // init

    public User() {
        username = "";
        password = "";
        checkAdmin = false;
    }
    public static final String USER_SESSION_KEY = "user";
    private String username;
    private String password;
    private Boolean checkAdmin;
//    List<SEC_ETT_Right> listRight;

    public String validateUser() throws Exception {
        FacesContext context = FacesContext.getCurrentInstance();
        SEC_ETT_User user = getUser();
        if (user.getPASSWORD() != null) {
            if (!user.getPASSWORD().equals(password)) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_ERROR, "Login failed",
                        "Wrong password");
                context.addMessage(null, message);
                return null;
            } else {
                validateGroup(user.getUSER_ID());
//                listRight = checkRightByGroupID(group.getGROUP_ID());
//                for (int i = 0; i < listRight.size(); i++) {
//                }
//                context.getExternalContext().getSessionMap()
//                        .put(USER_SESSION_KEY, user);
                return "main-index";
            }
        } else {
            FacesMessage message = new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Login failed", "User '"
                    + username + "' does not exist");
            context.addMessage(null, message);
            return null;
        }
    }

    public void validateGroup(int userId) {
        try {
            SEC_ETT_Group group = SEC_BU_USER_GROUP.getGroupByUserID(userId);
            if (group.getGROUP_NAME().equals("admin")) {
                checkAdmin = true;
            } else {
                checkAdmin = false;
            }
        } catch (NoResultException nre) {
            nre.toString();
        }
    }
//    public List<SEC_ETT_Right> checkRightByGroupID(int groupId) {
//        try {
//            List<SEC_ETT_Right> ListRight = SEC_BU_GROUP_RIGHT.getAllRightByGroupID(groupId);
//            return ListRight;
//        } catch (NoResultException nre) {
//            return null;
//        }
//    }

    public String logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
                .getExternalContext().getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return "login";
    }

    private SEC_ETT_User getUser() {
        try {
            SEC_ETT_User user = SEC_BU_User.getPassWordbyUserName(username);
            return user;
        } catch (NoResultException nre) {
            return null;
        }
    }

    public void setCheckAdmin(Boolean checkAdmin) {
        this.checkAdmin = checkAdmin;
    }

    public Boolean getCheckAdmin() {
        return checkAdmin;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}